<?php

namespace app\Api\controller;

use Rtgm\sm\RtSm2;

class Allinpay extends ApiBase
{
    public function test1027()
    {
        $params = [
            'reqTraceNum' => uniqid(),
            'signNum'     => '#yunBizUserId_B2C#',
            'infoType'    => '1',
        ];
        $utils = new Service('/tm/handle');
        $res = $utils->assembleRequest('1027',json_encode($params));
        return show(config("status.success"),"回调数据",$res);
    }

    public function upload(){
        // 如果有文件要上传
        $filePath = public_path().'storage/images/20240925/156f10dd1dff8b17b89660d0877d5281.jpg';
        // 初始化cURL会话
        $curl = curl_init();
        // 设置cURL选项
        curl_setopt($curl, CURLOPT_URL, "http://116.228.64.55:28082/yst-service-api/file/upload");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        // 准备POST数据
        $postFields = [
            'appId' => '21826541548215644162',
            'fileType' => '3',
            'md5' => md5_file($filePath),
        ];
        // 假设你已经有了其他参数，现在生成签名
        $timestamp = time();
        $postFields['timestamp'] = $timestamp;

        // 构造待签名的字符串
        $srcMsg = $postFields['appId'] . $postFields['fileType']  . $postFields['md5'] . $postFields['timestamp'];
        $ssm2 = new RtSm2("base64");
        $postFields['sign'] = trim($ssm2->doSign($srcMsg, config("yst.sm2PrivateAllinpay")));
        if (file_exists($filePath)) {
            $cFile = curl_file_create($filePath);
            $postFields['file'] = $cFile;
        } else {
            die('File not found.');
        }
        //print_r($postFields);
        // 添加POST字段
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
        // 执行cURL请求
        $response = curl_exec($curl);
        // 关闭cURL会话
        curl_close($curl);
        // 处理响应
        echo $response;
    }
}
